//*************************************************************************************
/** \file adc_test.cpp
 *      This file contains a test program which checks that the A/D converter on the
 *      AVR processor is working correctly. 
 *
 *  Revisions:
 *    \li  00-00-00  The Earth cooled, followed by the appearance of sliced bread
 *
 *  License
 *    This file copyright 2007-2012 by somebody. It is released under the Lesser GNU
 *    public license, version 2. It is intended for educational use only, but the
 *    user is free to use it for any purpose permissible under the LGPL. The author
 *    has no control over the use of this file and cannot take any responsibility
 *    for any consequences of this use. 
 */
//*************************************************************************************

											// System headers included with < >
#include <stdlib.h>							// Standard C library
											// User written headers included with " "
#include "rs232int.h"						// Include header for serial port class
#include "avr_adc.h"						// Include header for the A/D class
#include "stl_timer.h"


//--------------------------------------------------------------------------------------
/** The main function is the "entry point" of every C program, the one which runs first
 *  (after standard setup code has finished). For mechatronics programs, main() runs an
 *  infinite loop and never exits. 
 */

int main ()
{
	volatile uint32_t dummy;				// Used as a not-smart delay loop counter
	uint16_t conversion;					// Data from the A/D

	// Create a serial port object. Test voltages will be printed to this port, which
	// should be hooked up to a dumb terminal program like minicom or GTKterm. 
	// IMPORTANT: 
	//   The parameters are (9600, 1) for the ME405 board and (9600, 0) for an AVR when
	//   you put it on a breadboard and use the RXD and TXD or RXD0 and TXD0 pins
	rs232 the_serial_port (9600, 0);
	task_timer my_timer;

	TCCR1A = 0x00;						// Set to normal 16-bit counting mode
	TCCR1B = (1 << CS12);				// Set prescaler to main clock / 8
	
	// Say hello. Hey, look! Something else you need to fix!
	the_serial_port << endl << "Analog to Digital NON-Test Program v0.001" << endl;

	// Create an ADC (Analog to Digital Converter) object. This object is given a
	// pointer to the serial port object so that it can print debugging information
	avr_adc my_adc (&the_serial_port);

	sei ();
	
	// Run the main scheduling loop, in which the action to run is done repeatedly.
	// In the future, we'll run tasks here; for now, just do things in a simple loop
	while (true)
	{
	  	char input_char;					// A character typed by the user

		// The dummy counter is used to slow down the rate at which stuff is printed
		// on the terminal. Normally this would not be used (a task timer would be 
		// used instead) but this is OK for a very simple test program
		if (++dummy >= 600000UL)
		{
			dummy = 0;

			// Cause an analog to digital conversion to take place, and print the
			// result, except there is no useful result yet
			//conversion = my_adc.read_once (0);
			//the_serial_port << "A/D got: " << conversion << endl;
			//the_serial_port << "A/D prints itself: " << my_adc << endl;
		}
		
				// Check if the user has typed something.
		if (the_serial_port.check_for_char ())
		{
			input_char = the_serial_port.getchar ();

			// If it's a 'c', clear the counter to a reading of zero; 
			if (input_char == 't')
			{
				
				the_serial_port << endl << "time: " << my_timer.get_time_now() << endl;
			}
		}
	}

	return (0);
}
